package com.hamizhong.iflowers.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.hamizhong.iflowers.db.dao.hibernate.ITargetDao;
import com.hamizhong.iflowers.db.entity.Target;
import com.hamizhong.iflowers.service.ITargetService;
import com.hamizhong.iflowers.service.base.MyBaseService;
import com.hamizhong.iflowers.utils.MyErrorMsgConstants;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.IObjectVO;
import com.hamizhong.iflowers.web.vo.TargetVO;

public class TargetServiceImpl extends MyBaseService implements ITargetService {
	
	private ITargetDao iTargetDao = null;

	public ITargetDao getiTargetDao() {
		return iTargetDao;
	}
	public void setiTargetDao(ITargetDao iTargetDao) {
		this.iTargetDao = iTargetDao;
	}
	
	public IOPResultDto<IObjectVO> findAll(Integer pageNum, int pageSize) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		List<Target> targetList = new ArrayList<Target>();
		targetList = iTargetDao.findTarget(pageNum, pageSize);
		int totalTargetCnt = iTargetDao.findTotalTargetCnt();
		List<TargetVO> targetVOList = new ArrayList<TargetVO>();
		TargetVO targetVO = null;
		for (Target target : targetList) {
			targetVO = new TargetVO();
			BeanUtils.copyProperties(target, targetVO);
			targetVOList.add(targetVO);
		}
		IObjectVO iObjectVO = new IObjectVO();
		iObjectVO.setTargetVOList(targetVOList);
		iObjectVO.setTotalTargetCnt(totalTargetCnt);
		dto.addToResultList(iObjectVO);
		dto.setSuccess(true);
		return dto;
	}
	public IOPResultDto<IObjectVO> addTarget(TargetVO targetVO) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		Target target = new Target();
		try {
			BeanUtils.copyProperties(targetVO, target);
			iTargetDao.add(target);
			dto.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			dto.setSuccess(false);
			dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_ADD_OPERATE);
		}
		return dto;
	}
	public IOPResultDto<IObjectVO> updateTarget(TargetVO targetVO) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		Target target = new Target();
		try {
			BeanUtils.copyProperties(targetVO, target);
			iTargetDao.update(target);
			dto.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			dto.setSuccess(false);
			dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_UPDATE_OPERATE);
		}
		return dto;
	}
	public IOPResultDto<IObjectVO> deleteTarget(TargetVO targetVO) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		try {
			String targetId = targetVO.getTargetId();
			Target target = iTargetDao.findById(targetId);
			if(null != target) {
				this.iTargetDao.delete(target);
				dto.setSuccess(true);
			}else {
				dto.setSuccess(false);
				dto.setErrorMSG(MyErrorMsgConstants.NOT_EXIST_DATA_FOR_OPERATE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			dto.setErrorMSG(MyErrorMsgConstants.ERR_SYSTEM);
			dto.setSuccess(false);
		}
		return dto;
	}
	

}
